package com.yuji.aacar.net.interceptor;

import com.yuji.aacar.core.cores.utils.LogUtil;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * <li>Date: 2017/4/20 </li>
 * <li>Description: </li>
 */
public class RequestInterceptor implements Interceptor {
    private String serviceName;
    public RequestInterceptor(String serviceName) {
        this.serviceName=serviceName;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        FormBody formBody = (FormBody) original.body();
        String body = formBody.size() > 1 ? formBody.value(0) : "";
        String header = formBody.size() > 1 ? formBody.value(1) : "";
        LogUtil.e("Interceptor formBody",formBody.value(0));
        LogUtil.e("Interceptor formHead",formBody.value(1));
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , body);
        Request request = new Request.Builder()
                .addHeader("mobilehead", header)
                .url(original.url())//请求的url
                .post(requestBody)
                .build();

        String isWebView = formBody.size() > 1 ? formBody.value(2) : "";
        LogUtil.e("Interceptor header",header);
        LogUtil.e("Interceptor isWebView",isWebView);
//        LogUtil.e("http_header",request.headers().get("Host"));
        LogUtil.e("request_url", request.url() + "");
        return chain.proceed(request);
    }
}
